/*
 *  Copyright 2011-2012 Redia A/S
 * 
 *  This file is part of Redias Android Utilities.
 *
 *  Redias Android Utilities is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redias Android Utilities is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redias Android Utilities.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.lib.model;

import java.text.ParseException;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

public class AbsoluteId implements Parcelable {
	private String id;
	private String customerId;
	private String absIdStr;
	
	public AbsoluteId(String absId) throws RuntimeException {
		AbsoluteId id = null;
		try {
			id = AbsoluteId.fromString(absId);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		this.id = id.id;
		this.customerId = id.customerId;
		this.absIdStr = this.customerId + "::" + this.id;
		this.validate();
	}
	
	public AbsoluteId(String customerId, String id) throws RuntimeException {
		this.id = id;
		this.customerId = customerId;
		this.absIdStr = this.customerId + "::" + this.id;
		this.validate();
	}
	
	private AbsoluteId() {
		this.id = "";
		this.customerId = "";
		this.absIdStr = "";
	}
	
	private void validate() throws RuntimeException {
		if (customerId.length()<=0 || id.length()<=0) {
			throw new RuntimeException("Invalid AbsoluteId created");
		}
	}
	
	public static AbsoluteId fromString(String absId) throws ParseException {
		String[] parts = TextUtils.split(absId, "::");
		AbsoluteId aid = new AbsoluteId();
		if (parts.length!=2) {
			throw new ParseException("Failed to parse " + absId + " as absoluteId", 1);	
		}
		if (parts[0].length()<=0) {
			throw new ParseException("Invalid customer id in absolute id string " + absId, 1);
		}
		if (parts[1].length()<=0) {
			throw new ParseException("Invalid id in absolute id string " + absId, 1);
		}
		aid.customerId = parts[0];
		aid.id = parts[1];
		aid.absIdStr = aid.customerId + "::" + aid.id;
		return aid;
	}
	
	
	public int hashCode() {
		return absIdStr.hashCode();
	}
	
	public boolean equals(Object o) {
		if (o instanceof AbsoluteId)  {
			return this.absIdStr.equals(((AbsoluteId)o).absIdStr);
		} else {
			return false;
		}
	}
	
	public String toString() {
		return this.absIdStr;
	}
	
	public String getId() {
		return this.id;
	}
	
	public String getCustomerId() {
		return this.customerId;
	}

	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(customerId);
		dest.writeString(id);
	}
	
	public static final Parcelable.Creator<AbsoluteId> CREATOR = new Parcelable.Creator<AbsoluteId>() {

		public AbsoluteId createFromParcel(Parcel source) {
			String customerId = source.readString();
			String id = source.readString();
			AbsoluteId aid = new AbsoluteId(customerId, id);
			return aid;
		}

		public AbsoluteId[] newArray(int size) {
			return new AbsoluteId[size];
		}
		
	};
}
